﻿using System;
using System.Web.UI.WebControls;
using System.Globalization;
using ContractManagementSystem.BusinessLogic;


namespace ContractManagementSystem
{
    public partial class ViewLog : System.Web.UI.Page
    {
        static readonly PagedDataSource PagedDataSource = new PagedDataSource();
        static int _pageNumber;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TypeLog.Items.Add("View All");
                TypeLog.Items.Add("EventLog");
                TypeLog.Items.Add("ErrorLog");
                ViewAllLog();
            }
        }

        public void ViewAllLog()
        {
            if (!String.IsNullOrEmpty(FromDateTextBox.Text) && !String.IsNullOrEmpty(ToDateTextBox.Text))
            {
                var fromdate = DateTime.Parse(FromDateTextBox.Text, CultureInfo.CurrentCulture);
                var todate = DateTime.Parse(ToDateTextBox.Text, CultureInfo.CurrentCulture);
                PagedDataSource.DataSource = LogBusiness.SearchLog(TypeLog.SelectedIndex, fromdate, todate).DefaultView;
                PagedDataSource.PageSize = 15;
                PagedDataSource.CurrentPageIndex = _pageNumber;
                PagedDataSource.AllowPaging = true;
                if (PagedDataSource.IsFirstPage)
                {
                    FirstPageButton.Visible = false;
                    PreviewPageButton.Visible = false;
                    NextPageButton.Visible = true;
                    LastPageButton.Visible = true;
                }
                if (PagedDataSource.IsLastPage)
                {
                    LastPageButton.Visible = false;
                    NextPageButton.Visible = false;
                    PreviewPageButton.Visible = true;
                    FirstPageButton.Visible = true;
                }
                PageLabel.Text = (_pageNumber + 1) + "/" + PagedDataSource.PageCount;
                ViewAllLogSystem.DataSource = PagedDataSource;
                ViewAllLogSystem.DataBind();
            }
            else
            {
                PagedDataSource.DataSource = LogBusiness.ViewLog(TypeLog.SelectedIndex).DefaultView;
                PagedDataSource.PageSize = 15;
                PagedDataSource.CurrentPageIndex = _pageNumber;
                PagedDataSource.AllowPaging = true;
                if (PagedDataSource.IsFirstPage)
                {
                    FirstPageButton.Visible = false;
                    PreviewPageButton.Visible = false;
                    NextPageButton.Visible = true;
                    LastPageButton.Visible = true;
                }
                if (PagedDataSource.IsLastPage)
                {
                    LastPageButton.Visible = false;
                    NextPageButton.Visible = false;
                    PreviewPageButton.Visible = true;
                    FirstPageButton.Visible = true;
                }
                PageLabel.Text = (_pageNumber + 1) + "/" + PagedDataSource.PageCount;
                ViewAllLogSystem.DataSource = PagedDataSource;
                ViewAllLogSystem.DataBind();
            }
            
        }


        protected void ViewAllLogSystem_OnItemDeleting(object sender, EventArgs e)
        {

        }

        protected void ViewAllLogSystem_OnItemEditing(object sender, EventArgs e)
        {

        }

        protected void TypeLogSelectedIndexChanged(object sender, EventArgs e)
        {
            ViewAllLog();
        }

        protected void SearchButtonClick(object sender, EventArgs e)
        {
            ViewAllLog();
        }

        protected void ViewAllLogSystemItemCommand(object sender, ListViewCommandEventArgs e)
        {

        }

        protected void ViewAllLogSystemSorting(object sender, ListViewSortEventArgs e)
        {
            ViewAllLogSystem.Sort("Content", SortDirection.Ascending);
        }

        protected void LastPageButton_Click(object sender, EventArgs e)
        {
            _pageNumber = PagedDataSource.PageCount - 1;
            ViewAllLog();
            LastPageButton.Visible = false;
        }

        protected void FirstPageButton_Click(object sender, EventArgs e)
        {
            _pageNumber = 0;
            ViewAllLog();
            FirstPageButton.Visible = false;
        }

        protected void PreviewPageButton_Click(object sender, EventArgs e)
        {
            if (!PagedDataSource.IsFirstPage)
            {
                _pageNumber--;
                ViewAllLog();
                NextPageButton.Visible = true;
                LastPageButton.Visible = true;
            }
        }

        protected void NextPageButton_Click(object sender, EventArgs e)
        {
            if (!PagedDataSource.IsLastPage)
            {
                _pageNumber++;
                ViewAllLog();
                PreviewPageButton.Visible = true;
                FirstPageButton.Visible = true;
            }
        }
    }
}
